/**
 * Copyright ©2012-2018 HopeHaiyang Corporation, All Rights Reserved
 * http://www.wuhanhope.com
 */
package hope.platform.data.web;

import hope.platform.data.domain.HopeException;
import hope.platform.data.server.data.header.HopeExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 *         Create at 2018/7/20 14:47
 */
@RestController
@RequestMapping("/exception")
public class HopeExceptionController {

    @Autowired
    private HopeExceptionService service;

    @RequestMapping(method = RequestMethod.POST)
    public HopeException addException(@RequestBody HopeException exception) throws Exception {
        return service.addHopeException(exception);
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<HopeException> getAll() throws Exception {
        return service.getAll();
    }

    @RequestMapping(value = "/all/beginTime/endTime/{beginTime}/{endTime}", method = RequestMethod.GET)
    public List<HopeException> getByGmtCreateTimeBetween(@PathVariable String beginTime,
                                                         @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return service.getByGmtCreateBetween(beginDate,endDate);
    }

    @RequestMapping(value = "/exceptionType/{exceptionType}", method = RequestMethod.GET)
    public List<HopeException> getExceptionByType(@PathVariable String exceptionType) throws Exception {
        return service.getByExceptionType(exceptionType);
    }

    @RequestMapping(value = "/exceptionType/serialNumber/{exceptionType}/{serialNumber}", method = RequestMethod.GET)
    public List<HopeException> getExceptionByTypeAndSerialNumber(@PathVariable String exceptionType,
                                                                 @PathVariable String serialNumber) throws Exception {
        return service.getByExceptionTypeAndSerialNumber(exceptionType, serialNumber);
    }

    @RequestMapping(value = "/exceptionType/serialNumber/dataName/{exceptionType}/{serialNumber}/{dataName}"
            , method = RequestMethod.GET)
    public List<HopeException> getExceptionByTypeAndSerialNumber(@PathVariable String exceptionType,
                                                                 @PathVariable String serialNumber,
                                                                 @PathVariable String dataType) throws Exception {
        return service.getByExceptionTypeAndSerialNumberAndDataName(exceptionType, serialNumber, dataType);

    }

    @RequestMapping(value = "/serialNumber/{serialNumber}/dataName/{dataName}" +
            "/beginTime/{beginTime}/endTime/{endTime}", method = RequestMethod.GET)
    public List<HopeException> getBySerialNumberAndDataNameAndGmtCreateBetweenOrderByGmtCreateDesc(
            @PathVariable String serialNumber,
            @PathVariable String dataName,
            @PathVariable String beginTime,
            @PathVariable String endTime) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return service.getBySerialNumberAndDataNameAndGmtCreateBetweenOrderByGmtCreateDesc(
                serialNumber, dataName, beginDate, endDate);
    }

    @RequestMapping(value = "/count/serialNumber/{serialNumber}/dataName/{dataName}" +
            "/beginTime/{beginTime}/endTime/{endTime}", method = RequestMethod.GET)
    public long countBySerialNumberAndDataNameAndGmtCreateBetween(
            @PathVariable String serialNumber,
            @PathVariable String dataName,
            @PathVariable String beginTime,
            @PathVariable String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = sdf.parse(beginTime);
        Date endDate = sdf.parse(endTime);
        return service.countBySerialNumberAndDataNameAndGmtCreateBetween(
                serialNumber, dataName, beginDate, endDate);
    }

    @RequestMapping(value = "/serialNumber/{serialNumber}", method = RequestMethod.GET)
    public List<HopeException> getExceptionBySerialNumber(@PathVariable String serialNumber) throws Exception {
        return service.getBySerialNumber(serialNumber);
    }

}
